package util;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class CommandsStorage {
    private static Map<String, String> commands;

    public static void parseCommands() {
        String filename = "command.xml";
        commands = new HashMap<String, String>();

        try {
            SAXReader reader = new SAXReader();
            Document document = reader.read(filename);
            Element rootElement = document.getRootElement();

            for (Iterator iterator = rootElement.elementIterator("bean"); iterator.hasNext(); ) {
                Element element = (Element) iterator.next();
                commands.put(element.attribute("id").getValue(), element.attribute("class").getValue());
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    public static String get(String cmd) {
        return commands.get(cmd);
    }

}
